/*
 *  Copyright (C) void Team 2010 Pedro Batista
 * 
 *  http://code.google.com/p/capacitec
 * 
 *  This file is part of capacitec.
 * 
 *  capacitec is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  capacitec is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with capacitec.  If not, see <http://www.gnu.org/licenses/>.
 */
package dao;

import br.com.caelum.vraptor.ioc.Component;
import entidades.AlunoSubProgramaTurma;
import entidades.AlunoSubProgramaTurmaPK;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author pedro
 */
@Component
public class AlunoSubProgramaTurmaDao {

	private final Session session;

	/**
	 * 
	 * @param session
	 */
	public AlunoSubProgramaTurmaDao(Session session) {
		this.session = session;
	}

	/**
	 * Salva um novo relacionamento entre aluno e turma.
	 * @param alunoSubProgramaTurma
	 */
	public void salva(AlunoSubProgramaTurma alunoSubProgramaTurma) {
		Transaction tx = session.beginTransaction();
		session.save(alunoSubProgramaTurma);
		tx.commit();
	}

	/**
	 * Apaga todas as ocorrencias de alunoId em alunoSubprogramaTurma.
	 * @param alunoId Id to aluno a ser apagado.
	 */
	public void apaga(Integer alunoId) {
		SQLQuery query = session.createSQLQuery("DELETE FROM aluno_subprograma_turma WHERE aluno_subprograma_aluno_pessoa_id = :pessoa_id");
		query.setInteger("pessoa_id", alunoId).executeUpdate();
	}

	/**
	 * Carrega o relacionamento entre aluno, subprograma e turma.
	 * @param alunoSubProgramaTurmaPK
	 * @return
	 */
	public AlunoSubProgramaTurma carrega(AlunoSubProgramaTurmaPK alunoSubProgramaTurmaPK) {
		AlunoSubProgramaTurma alunoSubProgramaTurma;
		alunoSubProgramaTurma = (AlunoSubProgramaTurma) session.load(AlunoSubProgramaTurma.class, alunoSubProgramaTurmaPK);
		return alunoSubProgramaTurma;
	}

	/**
	 * Carrega todos os relacionamentos aluno, subprograma e turma existentes
	 * no banco.
	 * @return Lista com os relancimentos encontrados.
	 */
	public List<AlunoSubProgramaTurma> carrega() {
		return session.createCriteria(AlunoSubProgramaTurma.class).list();
	}

	/**
	 * Recarrega com os dados atuais do banco.
	 * @param alunoSubProgramaTurma
	 */
	public void recarrega(AlunoSubProgramaTurma alunoSubProgramaTurma) {
		session.refresh(alunoSubProgramaTurma);
	}
}
